import 'package:flutter/material.dart';
import 'package:flutterdome/components/file_preview_widget.dart';

class FilePreviewExamplePage extends StatelessWidget {
  const FilePreviewExamplePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('文件预览示例'),
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            const SizedBox(height: 20),
            const Text(
              '文件预览组件演示',
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 30),
            
            // 单图片预览示例
            _buildPreviewButton(
              context,
              '单图片预览',
              () => _showSingleImagePreview(context),
              Colors.blue,
            ),
            
            const SizedBox(height: 16),
            
            // 多图片预览示例
            _buildPreviewButton(
              context,
              '多图片预览',
              () => _showMultiImagePreview(context),
              Colors.green,
            ),
            
            const SizedBox(height: 16),
            
            // 视频预览示例
            _buildPreviewButton(
              context,
              '视频预览',
              () => _showVideoPreview(context),
              Colors.red,
            ),
            
            const SizedBox(height: 16),
            
            // 音频预览示例
            _buildPreviewButton(
              context,
              '音频预览',
              () => _showAudioPreview(context),
              Colors.purple,
            ),
            
            const SizedBox(height: 16),
            
            // PDF预览示例
            _buildPreviewButton(
              context,
              'PDF预览',
              () => _showPdfPreview(context),
              Colors.orange,
            ),
            
            const SizedBox(height: 16),
            
            // Markdown预览示例
            _buildPreviewButton(
              context,
              'Markdown预览',
              () => _showMarkdownPreview(context),
              Colors.teal,
            ),
            
            const SizedBox(height: 16),
            
            // HTML预览示例
            _buildPreviewButton(
              context,
              'HTML预览',
              () => _showHtmlPreview(context),
              Colors.pink,
            ),
            
            const SizedBox(height: 16),
            
            // 文本预览示例
            _buildPreviewButton(
              context,
              '文本预览',
              () => _showTextPreview(context),
              Colors.grey,
            ),
            
            const SizedBox(height: 16),
            
            // 未知文件预览示例
            _buildPreviewButton(
              context,
              '未知文件预览',
              () => _showUnknownFilePreview(context),
              Colors.black38,
            ),
            
            const SizedBox(height: 50),
          ],
        ),
      ),
    );
  }

  Widget _buildPreviewButton(
    BuildContext context,
    String title,
    VoidCallback onPressed,
    Color color,
  ) {
    return ElevatedButton(
      onPressed: onPressed,
      style: ElevatedButton.styleFrom(
        backgroundColor: color,
        padding: const EdgeInsets.symmetric(vertical: 16),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8),
        ),
      ),
      child: Text(
        title,
        style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
      ),
    );
  }

  // 单图片预览
  void _showSingleImagePreview(BuildContext context) {
    showFilePreview(
      context: context,
      file: FileInfo(
        path: 'https://picsum.photos/800/600',
        name: '示例图片.jpg',
        type: FileType.image,
        config: const FilePreviewConfig(title: '示例图片'),
      ),
    );
  }

  // 多图片预览
  void _showMultiImagePreview(BuildContext context) {
    final images = [
      FileInfo(
        path: 'https://picsum.photos/800/600?random=1',
        name: '图片1.jpg',
        type: FileType.image,
      ),
      FileInfo(
        path: 'https://picsum.photos/800/600?random=2',
        name: '图片2.jpg',
        type: FileType.image,
      ),
      FileInfo(
        path: 'https://picsum.photos/800/600?random=3',
        name: '图片3.jpg',
        type: FileType.image,
      ),
      FileInfo(
        path: 'https://picsum.photos/800/600?random=4',
        name: '图片4.jpg',
        type: FileType.image,
      ),
    ];

    showFilePreview(
      context: context,
      files: images,
      initialIndex: 0,
    );
  }

  // 视频预览
  void _showVideoPreview(BuildContext context) {
    showFilePreview(
      context: context,
      file: FileInfo(
        path: 'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4',
        name: '示例视频.mp4',
        type: FileType.video,
        config: const FilePreviewConfig(
          title: '示例视频',
          autoPlay: false,
          showControls: true,
          aspectRatio: 16 / 9,
        ),
      ),
    );
  }

  // 音频预览
  void _showAudioPreview(BuildContext context) {
    showFilePreview(
      context: context,
      file: FileInfo(
        path: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3',
        name: '示例音频.mp3',
        type: FileType.audio,
        config: const FilePreviewConfig(
          title: '示例音频',
          autoPlay: false,
        ),
      ),
    );
  }

  // PDF预览
  void _showPdfPreview(BuildContext context) {
    showFilePreview(
      context: context,
      file: FileInfo(
        path: 'https://www.africau.edu/images/default/sample.pdf',
        name: '示例文档.pdf',
        type: FileType.pdf,
        config: const FilePreviewConfig(title: '示例PDF文档'),
      ),
    );
  }

  // Markdown预览
  void _showMarkdownPreview(BuildContext context) {
    showFilePreview(
      context: context,
      file: FileInfo(
        path: 'https://raw.githubusercontent.com/felangel/markdown/master/example.md',
        name: '示例文档.md',
        type: FileType.markdown,
        config: const FilePreviewConfig(title: 'Markdown示例'),
      ),
    );
  }

  // HTML预览
  void _showHtmlPreview(BuildContext context) {
    showFilePreview(
      context: context,
      file: FileInfo(
        path: 'https://flutter.dev',
        name: 'Flutter官网.html',
        type: FileType.html,
        config: const FilePreviewConfig(title: 'Flutter官网'),
      ),
    );
  }

  // 文本预览
  void _showTextPreview(BuildContext context) {
    showFilePreview(
      context: context,
      file: FileInfo(
        path: 'https://raw.githubusercontent.com/dwyl/english-words/master/words.txt',
        name: '单词表.txt',
        type: FileType.text,
        config: const FilePreviewConfig(title: '文本文件示例'),
      ),
    );
  }

  // 未知文件预览
  void _showUnknownFilePreview(BuildContext context) {
    showFilePreview(
      context: context,
      file: FileInfo(
        path: 'sample.xyz',
        name: '未知文件.xyz',
        type: FileType.unknown,
        config: const FilePreviewConfig(title: '未知文件类型'),
      ),
    );
  }
}